
package com.ruipengkj.view.giftView;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TGift;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.service.TGiftService;
import com.ruipengkj.view.base.BasePanel;
/**
 * 说明：
 * 添加礼品
 * @Author：孙航
 * @Date：2012-07-02
 */
public class GiftCreatePanel extends BasePanel {
	
	private static final long serialVersionUID = 1L;
	private JLabelJTextFieldComponent giftID = null;//赠送编号
	private JLabelJTextFieldComponent commodityID = null;//商品编号
	private JLabelJTextFieldComponent money = null; //赠送金额
	private JLabelJTextFieldComponent type = null; //赠送类型
	private JLabelJTextFieldComponent value = null;//上阀
	
	private JButton sureButton = null; //确定
	private JButton cancelButton = null; //取消
	
	private TGiftService tGiftService;
	public GiftCreatePanel() {
		tGiftService=BuildSpringApplication.getInstance().getTGiftServiceImpl();
		this.addComponents();
		this.initListener();
	}
	
	private void addComponents() {
		JPanel main_jp=new JPanel();
		main_jp.setBorder(BorderFactory.createTitledBorder("添加礼品"));
		BoxLayout boxLayout=new BoxLayout(main_jp, BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		
		FlowLayout flowLayout=new FlowLayout(FlowLayout.LEFT);
		JPanel giftCreate_jp=new JPanel(flowLayout);
		commodityID = new JLabelJTextFieldComponent("  *", "赠送编号：",30);
		giftCreate_jp.add(commodityID);
		giftID = new JLabelJTextFieldComponent("   *", "商品编号：",30);
		giftID.getText().setText(UUID.GenUUID15());
		giftID.getText().setEditable(false);
		money = new JLabelJTextFieldComponent("   *", "赠送金额：", 30);
		type= new JLabelJTextFieldComponent("   *", "赠送类型：", 30);
		value = new JLabelJTextFieldComponent("   *", "上    阀：", 30);
		
		JPanel jp6 = new JPanel(flowLayout);
		sureButton = new JButton("确定(Ctrl+Y)");
		cancelButton = new JButton("取消");
		jp6.add(sureButton);
		jp6.add(cancelButton);
		main_jp.add(giftID);
		main_jp.add(giftCreate_jp);
		main_jp.add(money);
		main_jp.add(type);
		main_jp.add(value);
		main_jp.add(jp6);
		this.add(main_jp);
	}
	
	private void initListener(){
		sureButton.addActionListener(this);
		sureButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}

	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource()==sureButton){
			//提交
			String gmoney=money.getText().getText().trim();//赠送金额
			String gcommodityID=commodityID.getText().getText().trim();//赠送商品编号
			String gtype=type.getText().getText().trim(); //赠送类型
			String gvalue=value.getText().getText().trim();//上阀

			if(gcommodityID.equals("")){
				JOptionPane.showMessageDialog(null, "赠送商品编号不能为空");
				commodityID.getText().requestFocus();
				return;
			}
			
			if(gmoney.equals("")){
				JOptionPane.showMessageDialog(null, "赠送金额不能为空");
				money.getText().requestFocus();
				return;
			}
			if(gtype.equals("")||!(gtype.length()>0)){
				JOptionPane.showMessageDialog(null, "赠送类型不能为空");
				type.getText().requestFocus();
				return;
			}
			if(gvalue.equals("")){
				JOptionPane.showMessageDialog(null, "上阀不能为空");
				value.getText().requestFocus();
				return;
			}
			
			TGift tGift = new TGift();
			tGift.setGiftID(UUID.GenUUID15());
			tGift.setCommodityID(gcommodityID);
			tGift.setMoney(Double.parseDouble(gmoney));
			tGift.setType(Byte.parseByte(gtype));
			tGift.setValve(Double.parseDouble(gvalue));
			
			try {
				tGiftService.saveService(tGift);
				BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("添加礼品，礼品编号为：+"+commodityID.getText().getText().trim());
				JOptionPane.showMessageDialog(null, "操作成功");
				this.clear();
			} catch (Exception e1) {
				e1.printStackTrace();
				JOptionPane.showMessageDialog(null, "操作失败，请重新操作");
				this.clear();
			}
		}
	}
	
	public void clear(){
		commodityID.getText().setText("");
		money.getText().setText("");
		type.getText().setText("") ;
		value .getText().setText("");
		commodityID.getText().requestFocus();
	}
}
